package zhieasy.cn.xpu_helper.controller.common;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.common.WechatUser;
import zhieasy.cn.service.common.WechatUserRoleService;

/**
 * @author DaHang
 * @date 2020/8/16 16:33
 */
@Api(value = "/common/wechatUserRole" , tags = "common模块，给用户授予角色操作接口")
@RestController
@RequestMapping("/common/wechatUserRole")
public class WechatUserRoleController {

    @Autowired
    WechatUserRoleService wechatUserRoleService;

    @ApiOperation(
        value = "老师将学生的角色调整为班长等角色",
        notes = "只有老师角色才可以调用该接口，如果不是老师调用该接口会报异常" +
            "3为班长，6为老师助理",httpMethod = "POST")
    @ApiImplicitParam(paramType="query", name = "roleId",
        value = "角色Id，注意这个方法只能带有2个参数，3与6，3对应班长，6对应助理",
        required = true, dataType = "Long")
    @ApiResponses({
        @ApiResponse(code=700,message="没有访问权限,请联系管理员"),
        @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/setRoleForStudent", method= RequestMethod.POST)
    @PreAuthorize("hasRole('teacher') and #roleId == 3 || #roleId == 6 ")
    public ResultVO setRoleForStudent(Long roleId){
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        WechatUser wechatUser = (WechatUser) auth.getPrincipal();
        boolean b = wechatUserRoleService.setStudentRole(wechatUser, roleId);
        return ResultUtils.success();
    }



    @ApiOperation(
        value = "给普通用户授予学生的身份",
        notes = "给普通用户授予学生的身份")
    @ApiResponses({
        @ApiResponse(code=700,message="没有访问权限,请联系管理员"),
        @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/setStudentRoleForNormalUser", method= RequestMethod.POST)
    @PreAuthorize("hasRole('normal')")
    public ResultVO setStudentRoleForNormalUser(){
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        WechatUser wechatUser = (WechatUser) auth.getPrincipal();
        boolean b = wechatUserRoleService.setStudentRoleForNormalUser(wechatUser);
        return ResultUtils.success();
    }

    @ApiOperation(
        value = "给普通用户授予老师的身份",
        notes = "给普通用户授予老师的身份")
    @ApiResponses({
        @ApiResponse(code=700,message="没有访问权限,请联系管理员"),
        @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/setTeacherRoleForNormalUser", method= RequestMethod.POST)
    @PreAuthorize("hasRole('normal')")
    public ResultVO setTeacherRoleForNormalUser(){
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        WechatUser wechatUser = (WechatUser) auth.getPrincipal();
        boolean b = wechatUserRoleService.setTeacherRoleForNormalUser(wechatUser);
        return ResultUtils.success();
    }

}
